package com.leesche.netdoll.communication;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.leesche.framework.core.codec.HexUtil;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class ByteBufferLogHandler extends ChannelInboundHandlerAdapter {

	private static final Logger logger = LogManager.getLogger("server-buffer-logger");

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

		if (msg instanceof ByteBuf) {
			ByteBuf buf = (ByteBuf) msg;
			if (buf != null && buf.readableBytes() > 0) {
				byte[] inData = new byte[buf.readableBytes()];
				buf.getBytes(buf.readerIndex(), inData);
				logger.info(Logs.getMsg(ctx.channel().remoteAddress(), HexUtil.encodeHexString(inData), buf.readableBytes()));
				logger.info("上报信息0:{}",ctx.channel().remoteAddress());
				logger.info("上报信息1:{}",HexUtil.encodeHexString(inData));
				logger.info("上报信息2:{}",buf.readableBytes());
			}
		}

		super.channelRead(ctx, msg);
	}

}
